//
//  TutorialProjectViewController.m
//  TutorialProject
//
//  Created by Ondrej Rafaj on 5.8.09.
//  Copyright Home 2009. All rights reserved.
//

#import "TutorialProjectViewController.h"

@implementation TutorialProjectViewController

// ------ Tutorial code starts here ------

@synthesize logWindow;


// function for adding an 
- (void)addRowToLogWindow:(id)data {
	
	logWindow.text = [NSString stringWithFormat:@"Adding data: %@\r\n\r\n%@", data, logWindow.text]; // adding new row + 2x new line "\r\n"
	
}

- (IBAction)startParser:(UIButton *)sender {
	
	// getting path to the file
	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
	
	NSLog(@"Path: %@", filePath);
		
	// reading content of the file, don't use the [NSString stringWithContentsOfFile:@"/Your/Filepath/"]; as this is deprecated since iPhone SDK 3.0
	NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
	
	//NSLog(@"File content: %@", fileContent);
	
	// creating new parser
	SBJSON *parser = [[SBJSON alloc] init];
	
	// parsing the first level
	NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
	
	// getting the data from inside of "menu"
	NSDictionary *menu = (NSDictionary *) [data objectForKey:@"menu"];
	
	// getting the first value which is stored in the header
	NSString *header = (NSString *) [menu objectForKey:@"header"];
	[self addRowToLogWindow:[NSString stringWithFormat:@"Header -> %@", header]];
	
	// parsing all the items in to the NSArray
	NSArray *items = (NSArray *) [menu objectForKey:@"items"];
	
	// reading all the items in the array one by one
	for (id *item in items) {
		
		// if the item is NSDictionary (in this case ... different json file will probably have a different class)
		[self addRowToLogWindow:[NSString stringWithFormat:@"Item -> %@", (NSDictionary *) item]];
		
	}
	for (id *item in items) {
		
		// if the item is NSDictionary (in this case ... different json file will probably have a different class)
		NSLog(@"One item: %@", [NSString stringWithFormat:@"Item -> %@", (NSDictionary *) item]);
		
	}
	
	// releasing parser
	[parser release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	// removing the default data from our UITextView
	logWindow.text = @"";
	
    [super viewDidLoad];
	
}



// ------ Tutorial code ends here ------


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/





/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

// This function is for button which takes you to the xprogress.com website
- (IBAction) runXprogressComButton: (id) sender {
	NSURL *url = [[[NSURL alloc] initWithString: @"http://www.xprogress.com/"] autorelease];
	[[UIApplication sharedApplication] openURL:url];
}


- (void)dealloc {
    
	// ------ Tutorial code starts here ------
	
	[logWindow release];
	
	// ------ Tutorial code ends here ------
	
	[super dealloc];
}

@end
